﻿/**
 * @author Alex Malygin
 */
package server_common;

class ResponseHelper {
	
	public static function reportError(error: String) {
		php.Lib.print("<?xml version=\"1.0\" encoding=\"UTF-8\"?><error>" + StringTools.htmlEscape(error) + "</error>");
	}
	
	public static function reportSuccess(result: String) {
		php.Lib.print("<?xml version=\"1.0\" encoding=\"UTF-8\"?><response>" + result + "</response>");
	}
	
	public static function wrapObject(object: Dynamic, name = null) {
		var xml = (name == null) ? "" : "<" + StringTools.htmlEscape(name) + ">";
		for (field in Reflect.fields(object)) {
			if (StringTools.startsWith(field, "_")) {
				continue;
			}
			xml += ("<" + StringTools.htmlEscape(field) + ">" + StringTools.htmlEscape(Reflect.field(object, field)) + "</" + StringTools.htmlEscape(field) + ">");
		}
		xml += ((name == null) ? "" : ("</" + StringTools.htmlEscape(name) + ">"));
		return xml;
	}
	
	public static function wrapObjects(objects: Iterable<Dynamic> , name = null) {
		var xml = "";
			for (obj in objects) 
			{
				xml += (wrapObject(obj, name));
			}
		return xml;
	}
	
	public static function wrapIterator(it: Iterator<Dynamic>, name = null): String {
		var xml = "";
		while (it.hasNext()) {
			xml += (wrapObject(it.next(), name));
		}
		return xml;
	}
	
	public static function reportEmptyParam(name: String) {
		reportError("Пустой параметр " + name);
	}
}